import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreVerifyService extends BaseService<any> {
  private readonly logger = new Logger(CoreVerifyService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * createRecord (原方法名: create)
   * 对应 PHP: CoreVerifyService_core::create()
   * 逻辑类型: undefined - undefined
   * 注意: 为避免与BaseService方法冲突，已重命名
   */
  async createRecord(site_id: any[], member_id: any[], type: any[], param: any[]) {
    // 基于PHP真实逻辑: create
    // PHP原文: $param['member_id'] = (string)$member_id;         if (!array_key_exists($type, VerifyDict::getType())) throw new CommonException('VERIFY_TYPE_ERROR');...
param.member_id = (string)member_id;
        if (!array_key_exists(type, VerifyDict.getType())) throw new BusinessException('VERIFY_TYPE_ERROR');//核销类型错误
        //遇到错误直接抛出即可
        const result = event('VerifyCreate', [site_id: site_id, type: type, member_id: member_id, data: param)];
        foreach (result as item) {
            if(!!item)){
                const result = item;
                break;
            }
  }

  /**
   * getInfoByCode
   * 对应 PHP: CoreVerifyService_core::getInfoByCode()
   * 逻辑类型: undefined - undefined
   */
  async getInfoByCode(site_id: string, member_id: string, verify_code: string) {
    // 基于PHP真实逻辑: getInfoByCode
    // PHP原文: //获取核销码数据         $value = $this->getCodeData($verify_code);         //检测站点数据         if ($value['site_id'] != $site_id) throw new CommonException('VE...
//获取核销码数据
        const value = this.getCodeData(verify_code);
        //检测站点数据
        if (value.site_id != site_id) throw new BusinessException('VERIFY_CODE_EXPIRED'];//核销码已过期
        const data = event('VerifyCheck', value);

        if (!!data)) {
            foreach (data as datum) {
                if (!!datum)) {
                    const value = datum;
                    break;
                }
            }
  }

  /**
   * verify
   * 对应 PHP: CoreVerifyService_core::verify()
   * 逻辑类型: undefined - undefined
   */
  async verify(site_id: number, verify_code: number, verify_member_id: number) {
    // 基于PHP真实逻辑: verify
    // PHP原文: //获取核销码数据         $value = $this->getCodeData($verify_code);         //检测站点数据         if ($value['site_id'] != $site_id) throw new CommonException('VE...
//获取核销码数据
        const value = this.getCodeData(verify_code);
        //检测站点数据
        if (value.site_id != site_id) throw new BusinessException('VERIFY_CODE_EXPIRED'];//核销码已过期
        //检测核销员身份
        const verifierModel = this.verifierService;
        const verifier = verifierModel.where([['site_id', '=', value.site_id], ['member_id', '=', verify_member_id))).findOrEmpty().toArray(];
        if (!verifier)) throw new BusinessException('VERIFIER_NOT_EXIST');

        const verify_data = [
            site_id: value.site_id,
            code: verify_code,
            data: value.data,
            value: value.value,
            type: value.type,
            body: value.body,
            relate_tag: value.relate_tag,
            create_time: time(),
            verifier_member_id: verify_member_id,
        ];
        //核销
        event('Verify', verify_data]; //todo:相关核销业务回调
        const model = this.verifyService;
        model.create(verify_data];
        //是核销码失效
        this.clearCode(verify_code);

        return true;
    }

    /**
     * 核销(核销api调用)
     * @return true
     */
    async adminVerify(site_id, string verify_code)
    {
        //获取核销码数据
        const value = this.getCodeData(verify_code);
        //检测站点数据
        if (value.site_id != site_id) throw new BusinessException('VERIFY_CODE_EXPIRED');//核销码已过期

        const verify_data = [
            site_id: value.site_id,
            code: verify_code,
            data: value.data,
            value: value.value,
            type: value.type,
            body: value.body,
            relate_tag: value.relate_tag,
            create_time: time(),
            verifier_member_id: 0,
            is_admin: 1,
        ];
        //核销
        event('Verify', verify_data]; //todo:相关核销业务回调
        const model = this.verifyService;
        model.create(verify_data];
        //是核销码失效
        this.clearCode(verify_code);

        return true;
    }

    /**
     * 获取核销码信息
     * @param site_id
     * @param string member_id
     * @param string verify_code
     * @return */
    async adminGetInfoByCode(site_id, verify_code)
    {
        //获取核销码数据
        const value = this.getCodeData(verify_code);
        //检测站点数据
        if (value.site_id != site_id) throw new BusinessException('VERIFY_CODE_EXPIRED'];//核销码已过期
        const data = event('VerifyCheck', value);

        if (!!data)) {
            foreach (data as datum) {
                if (!!datum)) {
                    const value = datum;
                    break;
                }
            }
  }

  /**
   * adminVerify
   * 对应 PHP: CoreVerifyService_core::adminVerify()
   * 逻辑类型: undefined - undefined
   */
  async adminVerify(site_id: string, verify_code: string) {
    // 基于PHP真实逻辑: adminVerify
    // PHP原文: //获取核销码数据         $value = $this->getCodeData($verify_code);         //检测站点数据         if ($value['site_id'] != $site_id) throw new CommonException('VE...
//获取核销码数据
        const value = this.getCodeData(verify_code);
        //检测站点数据
        if (value.site_id != site_id) throw new BusinessException('VERIFY_CODE_EXPIRED');//核销码已过期

        const verify_data = [
            site_id: value.site_id,
            code: verify_code,
            data: value.data,
            value: value.value,
            type: value.type,
            body: value.body,
            relate_tag: value.relate_tag,
            create_time: time(),
            verifier_member_id: 0,
            is_admin: 1,
        ];
        //核销
        event('Verify', verify_data]; //todo:相关核销业务回调
        const model = this.verifyService;
        model.create(verify_data];
        //是核销码失效
        this.clearCode(verify_code);

        return true;
    }

    /**
     * 获取核销码信息
     * @param site_id
     * @param string member_id
     * @param string verify_code
     * @return */
    async adminGetInfoByCode(site_id, verify_code)
    {
        //获取核销码数据
        const value = this.getCodeData(verify_code);
        //检测站点数据
        if (value.site_id != site_id) throw new BusinessException('VERIFY_CODE_EXPIRED'];//核销码已过期
        const data = event('VerifyCheck', value);

        if (!!data)) {
            foreach (data as datum) {
                if (!!datum)) {
                    const value = datum;
                    break;
                }
            }
  }

  /**
   * adminGetInfoByCode
   * 对应 PHP: CoreVerifyService_core::adminGetInfoByCode()
   * 逻辑类型: undefined - undefined
   */
  async adminGetInfoByCode(site_id: any, verify_code: any) {
    // 基于PHP真实逻辑: adminGetInfoByCode
    // PHP原文: //获取核销码数据         $value = $this->getCodeData($verify_code);         //检测站点数据         if ($value['site_id'] != $site_id) throw new CommonException('VE...
//获取核销码数据
        const value = this.getCodeData(verify_code);
        //检测站点数据
        if (value.site_id != site_id) throw new BusinessException('VERIFY_CODE_EXPIRED'];//核销码已过期
        const data = event('VerifyCheck', value);

        if (!!data)) {
            foreach (data as datum) {
                if (!!datum)) {
                    const value = datum;
                    break;
                }
            }
  }

  /**
   * createCode
   * 对应 PHP: CoreVerifyService_core::createCode()
   * 逻辑类型: undefined - undefined
   */
  async createCode(verify_code: any, value: any, expire_time: any) {
    // 基于PHP真实逻辑: createCode
    // PHP原文: Cache::tag('verify')->set($verify_code, $value, $expire_time);     }      /**      * 清除核销      * @param $verify_code      * @return void      */     p...
Cache.tag('verify').set(verify_code, value, expire_time);
    }

    /**
     * 清除核销
     * @param verify_code
     * @return void
     */
    private async clearCode(verify_code)
    {
        Cache.delete(verify_code);
    }

    /**
     * 获取核销码数据缓存
     * @param verify_code
     * @return */
    private async getCodeData(verify_code)
    {
        const code_cache = Cache.get(verify_code, [)];

        if (!code_cache)) throw new BusinessException('VERIFY_CODE_EXPIRED');//核销码已过期
        return code_cache;
    }

}
  }

  /**
   * clearCode
   * 对应 PHP: CoreVerifyService_core::clearCode()
   * 逻辑类型: undefined - undefined
   */
  async clearCode(verify_code: any) {
    // 基于PHP真实逻辑: clearCode
    // PHP原文: Cache::delete($verify_code);     }      /**      * 获取核销码数据缓存      * @param $verify_code      * @return array      */     private function getCodeData(...
Cache.delete(verify_code);
    }

    /**
     * 获取核销码数据缓存
     * @param verify_code
     * @return */
    private async getCodeData(verify_code)
    {
        const code_cache = Cache.get(verify_code, [)];

        if (!code_cache)) throw new BusinessException('VERIFY_CODE_EXPIRED');//核销码已过期
        return code_cache;
    }

}
  }

  /**
   * getCodeData
   * 对应 PHP: CoreVerifyService_core::getCodeData()
   * 逻辑类型: undefined - undefined
   */
  async getCodeData(verify_code: any) {
    // 基于PHP真实逻辑: getCodeData
    // PHP原文: $code_cache = Cache::get($verify_code, []);          if (empty($code_cache)) throw new CommonException('VERIFY_CODE_EXPIRED');//核销码已过期         return ...
const code_cache = Cache.get(verify_code, [)];

        if (!code_cache)) throw new BusinessException('VERIFY_CODE_EXPIRED');//核销码已过期
        return code_cache;
    }

}
  }
}
